<template>
  <a-modal
    title="选择用户组"
    width="60%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @cancel="handleCancel"
    cancelText="关闭"
    okText="确定"
    @ok="handleOk"
    style="top:20px;"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :rules="validatorRules" :model="model" layout="vertical">
        <a-form-model-item label="用户组" prop="group">
          <a-select showSearch :filterOption="filterOption" mode="multiple" v-model="model.group" placeholder="请选择用户组">
            <a-select-option v-for="(item,index) in groupList" :value="item.groupCode" :key="index">
              {{item.groupName}}
            </a-select-option>
          </a-select>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import { getAction } from '@/api/manage'

  export default {
    name: 'UserGroupSelectModal',
    data() {
      return {
        visible: false,
        confirmLoading: false,
        model: {},
        groupList:[],
        validatorRules: {
          group: [{ required: true, message: '请选择用户组' }],
        },
        url: {
          queryAuditGroupList: '/process/extActProcess/queryAuditGroupList'
        }
      }
    },
    methods: {
      show() {
        this.visible = true
        this.userGroupConfigList();
        this.$nextTick(() => {
          this.$refs.form.resetFields()
          this.model = Object.assign({}, {})
        })
      },
      userGroupConfigList(){
        getAction(this.url.queryAuditGroupList,{}).then(res=>{
          if(res.success){
            this.groupList = res.result;
          }
        });
      },
      handleCancel() {
        this.visible = false
      },
      filterOption(input, option) {
        return (option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0)
      },
      handleOk() {
        const that = this
        // 触发表单验证
        this.$refs.form.validate((ok, err) => {
          if (ok) {
            let groups = that.model.group.join(",");
            that.$emit('selectGroup', groups)
            that.visible = false
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>